package com.yuanli.yuanliutils.yl_pay.utils;

import java.util.Map;
import java.util.Random;

public class PayUtil {
    /**
     * 获得app签名
     * @param map
     * @return
     */
    public static String genAppSign(Map<String, String> map, String wxApiKey) {
        StringBuilder sb = new StringBuilder();

        for (Map.Entry<String,String> entry :map.entrySet()){
            sb.append(entry.getKey());
            sb.append('=');
            sb.append(entry.getValue());
            sb.append('&');
        }
        /*for (int i = 0; i < map.size(); i++) {
            sb.append(params.get(i).getName());
            sb.append('=');
            sb.append(params.get(i).getValue());
            sb.append('&');
        }*/
        sb.append("key=");
        sb.append(wxApiKey);
        String str = sb.toString();
        String appSign = MD5.getMessageDigest(str.getBytes());
        return appSign == null ? null : appSign.toUpperCase();
    }

    /**得到随机字符串*/
    public static String genNonceStr() {
        Random random = new Random();
        return MD5.getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());
    }
}
